package com.mall.controller;

import com.mall.common.AuthCheck;
import com.mall.common.Result;
import com.mall.entity.User;
import com.mall.service.UserService;
import com.mall.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端用户管理
 */
@RestController
@RequestMapping("/admin/user")
public class AdminUserController {

    @Autowired
    private UserService userService;

    /**
     * 分页获取用户列表
     */
    @GetMapping("/list")
    @AuthCheck
    public Result<List<UserInfoVO>> listUsers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword
    ) {
        // TODO: 实现分页和模糊查询
        List<UserInfoVO> list = userService.listUsers(pageNum, pageSize, keyword);
        return Result.success(list);
    }

    /**
     * 新增用户
     */
    @PostMapping
    @AuthCheck
    public Result<String> addUser(@RequestBody User user) {
        userService.addUser(user);
        return Result.success("添加成功");
    }

    /**
     * 编辑用户
     */
    @PutMapping("/{id}")
    @AuthCheck
    public Result<String> updateUser(@PathVariable Long id, @RequestBody User user) {
        userService.updateUserByAdmin(id, user);
        return Result.success("更新成功");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @AuthCheck
    public Result<String> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return Result.success("删除成功");
    }

    /**
     * 根据ID获取用户
     */
    @GetMapping("/{id}")
    @AuthCheck
    public Result<UserInfoVO> getUser(@PathVariable Long id) {
        UserInfoVO userInfo = userService.getUserInfo(id);
        return Result.success(userInfo);
    }
}